package com.zxy.ziems.server.scene.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.ziems.server.device.service.DeviceService;
import com.zxy.ziems.server.scene.pojo.dto.SceneDTO;
import com.zxy.ziems.server.scene.pojo.param.SceneTreeParam;
import com.zxy.ziems.server.scene.pojo.vo.SceneTreeVO;
import com.zxy.ziems.server.scene.pojo.vo.SceneVO;
import com.zxy.ziems.server.scene.service.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采集管理-场景
 *
 * @author chdzq
 * @create 2023/3/21
 */
@RestController
@RequestMapping("/api")
public class SceneController {
    /**
     * 新增场景
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/scene/add")
    public JsonResponse<SceneVO> add(@Validated(value = SceneDTO.Add.class) @RequestBody SceneDTO dto) throws Exception {
        return JsonResponse.SUCCESS(service.add(dto));
    }

    /**
     * 更新场景
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/scene/update")
    public JsonResponse<SceneVO> update(@Validated(value = SceneDTO.Update.class) @RequestBody SceneDTO dto) throws Exception {
        return JsonResponse.SUCCESS(service.update(dto));
    }

    /**
     * 删除场景
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/scene/delete")
    public JsonResponse<?> delete(@Validated(value = SceneDTO.Select.class) @RequestBody SceneDTO dto) throws Exception {
        service.deleteById(dto.getId());
        return JsonResponse.SUCCESS();
    }

    /**
     * 查询场景信息
     *
     * @param dto 用户post参数
     * @return
     */
    @GetMapping("/scene/detail")
    public JsonResponse<SceneVO> detail(@Validated(value = SceneDTO.Select.class) SceneDTO dto) throws Exception {
        return JsonResponse.SUCCESS(service.viewById(dto.getId()));
    }

    /**
     * 场景树形结构查询
     *
     * @param param 查询参数
     * @return
     */
    @GetMapping("/scene/tree")
    public JsonResponse<List<SceneTreeVO>> tree(SceneTreeParam param) throws Exception {
        return JsonResponse.SUCCESS(service.getTreeBy(param));
    }

    /**
     * 查询场景信息(首页)
     *
     * @param dto 用户post参数
     * @return
     */
    @GetMapping("/scene/pageSelect")
    public JsonResponse<SceneVO> pageSelect(@Validated(value = SceneDTO.PageSelect.class) SceneDTO dto) {
        return JsonResponse.SUCCESS(service.viewByCode(dto.getCode()));
    }

    /**
     * 场景树形结构查询(告警点位)
     *
     * @param param 查询参数
     * @return
     */
    @GetMapping("/scene/treeAlarm")
    public JsonResponse<List<SceneTreeVO>> treeAlarm(SceneTreeParam param) throws Exception {
        return JsonResponse.SUCCESS(deviceService.getAlarmTreeBy(param));
    }

    /**
     * 场景列表查询
     *
     * @param sceneParentId 场景的父级节点 0 或是 null 为根节点
     * @return
     */
    @GetMapping("/scene/list")
    public JsonResponse<List<SceneVO>> list(@RequestParam(required = false) String sceneParentId) throws Exception {
        return JsonResponse.SUCCESS(service.listByParentId(sceneParentId));
    }

    private SceneService service;

    @Autowired
    public void setService(SceneService sceneService) {
        this.service = sceneService;
    }

    private DeviceService deviceService;

    @Autowired
    public void setService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }
}
